package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.EvaluateImageResponse
import com.gexiaobao.pigeon.app.model.param.OutChatParam
import com.gexiaobao.pigeon.app.model.param.SyncImOldMsgParam
import com.gexiaobao.pigeon.app.model.param.SyncParam
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2024/2/23 16:21
 * @Describe    :
 */
class MallImModel : BaseViewModel() {

    private var pageSize = 20
    private var pageNo: Int = 1

    /** 获取某频道消息 */
    val syncMessageImResult: MutableLiveData<Any> = MutableLiveData()
    fun getSyncMessageIm(body: SyncImOldMsgParam) {
        request({ apiService.getMessageSync(body) }, {
            syncMessageImResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 同步最近通话 */
    val syncResult: MutableLiveData<Any> = MutableLiveData()
    fun getSyncContact(body: SyncParam) {
        request({ apiService.sync(body) }, {
            syncResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 上传图片 */
    val uploadImageResult: MutableLiveData<EvaluateImageResponse> = MutableLiveData()
    fun uploadFile(imageFile: File) {
        val body: RequestBody = imageFile.asRequestBody("multipart/form-data".toMediaTypeOrNull()) //表单类型
        val builder = MultipartBody.Builder().setType(MultipartBody.FORM)//表单类型
        builder.addFormDataPart("upLoadFile", imageFile.name, body) //添加图片数据，body创建的请求体
        val requestBody = builder.build()

        request({ apiService.uploadFile(requestBody) }, {
            uploadImageResult.value = it
        }, {
            RxToast.showToast(it.message)
        })
    }
}